package com.victor.controller.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.GlCollection;
import com.victor.entity.GlComments;
import com.victor.entity.GlPraise;
import com.victor.entity.GlReply;
import com.victor.entity.GlcommentsPraise;
import com.victor.entity.Tourismgl;
import com.victor.service.TourismService;
import com.victor.utils.ClassFilter;
import com.victor.utils.Msg;

@RestController
@RequestMapping("/strategy")
public class GlController {
	@Autowired
	private TourismService tourismService;

	/**
	 * 添加/取消收藏
	 * 
	 * @param glCollection
	 * @return
	 */
	@RequestMapping("/collection")
	public Msg addCollection(GlCollection glCollection) {
		String[] str = { "uid", "glid" };
		boolean empty = ClassFilter.somePropertyIsEmpty(glCollection, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		GlCollection selCollection = tourismService.selCollection(glCollection);
		if (selCollection != null) {
			boolean delCollection = tourismService.delCollection(selCollection.getId());
			if (!delCollection) {
				return Msg.fail("删除收藏失败");
			}
			return Msg.success().add("return", "取消收藏成功");
		} else {
			boolean b = tourismService.addGlCollection(glCollection);
			if (!b) {
				return Msg.fail("添加收藏失败");
			}
			return Msg.success().add("return", "收藏成功");
		}

	}

	/**
	 * 点赞/取消点赞
	 * 
	 * @param glPraise
	 * @return
	 */
	@RequestMapping("/praise")
	public Msg praise(GlPraise glPraise) {
		String[] str = { "uid", "glid" };
		boolean empty = ClassFilter.somePropertyIsEmpty(glPraise, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		GlPraise praise = tourismService.selByGlpraise(glPraise);
		if (praise != null) {
			boolean delPraise = tourismService.delPraise(praise.getId());
			if (!delPraise) {
				return Msg.fail("删除失败");
			}
			return Msg.success().add("return", "取消点赞成功");
		} else {
			boolean insertpraise = tourismService.insertpraise(glPraise);
			if (!insertpraise) {
				return Msg.fail("添加失败");
			}
			return Msg.success().add("return", "点赞成功");
		}
	}

	/**
	 * 攻略评论添加
	 * 
	 * @param glComments
	 * @return
	 */
	@RequestMapping("/addcomments")
	public Msg addGlComments(@RequestBody GlComments glComments) {
		String[] str = { "uid", "glid", "content" };
		boolean empty = ClassFilter.somePropertyIsEmpty(glComments, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		boolean insertComments = tourismService.insertComments(glComments);
		if (!insertComments) {
			return Msg.fail("添加失败");
		}
		if (glComments.getPhotos() != null ) {
			for (int i = 0; i < glComments.getPhotos().size(); i++) {
				glComments.getPhotos().get(i).setCid(String.valueOf(glComments.getId()));
				boolean insertCommentsPhoto = tourismService.insertCommentsPhoto(glComments.getPhotos().get(i));
				if (!insertCommentsPhoto) {
					return Msg.fail("添加图片失败");
				}
			}
		}
		
		return Msg.success();
	}

	/**
	 * 回复评论
	 * @param glReply
	 * @return
	 */
	@RequestMapping("/addreply")
	public Msg addReplyComments(GlReply glReply) {
		String[] str = { "uid", "cid", "replycontent" };
		boolean empty = ClassFilter.somePropertyIsEmpty(glReply, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		boolean insertReply = tourismService.insertReply(glReply);
		if (!insertReply) {
			return Msg.fail("添加失败");
		}
		return Msg.success();
	}
	
	/**
	 * 评论点赞/取消点赞
	 * @param glcommentsPraise
	 * @return
	 */
	@RequestMapping("/commentspraise")
	public Msg commentPraise(GlcommentsPraise glcommentsPraise) {
		String[] str = { "uid", "gcid" };
		boolean empty = ClassFilter.somePropertyIsEmpty(glcommentsPraise, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		GlcommentsPraise selCommentsPraise = tourismService.selCommentsPraise(glcommentsPraise);
		if (selCommentsPraise == null) {
			boolean insertCommentsPraise = tourismService.insertCommentsPraise(glcommentsPraise);
			if (!insertCommentsPraise) {
				return Msg.fail("添加点赞失败");
			}
			Integer gcid = tourismService.selPraiseCountByGcid(glcommentsPraise.getGcid());
			return Msg.success().add("return", "点赞成功").add("count", gcid);
		}else {
			boolean delCommentsPraise = tourismService.delCommentsPraise(selCommentsPraise.getId());
			if (!delCommentsPraise) {
				return Msg.fail("删除评论点赞失败");
			}
			Integer gcid = tourismService.selPraiseCountByGcid(glcommentsPraise.getGcid());
			return Msg.success().add("return", "取消点赞").add("count", gcid);
		}
	}
	
	/**
	 * 攻略详情
	 * @param glid
	 * @return
	 */
	@RequestMapping("/appxiangqing")
	public Msg glXiangqing(Integer glid,String uid) {
		if (glid == null) {
			return Msg.fail("入参失败");
		}
		if (uid == null || uid.isEmpty()) {
			Tourismgl gTourismgl = tourismService.selTourismById(glid);
			return Msg.success().add("info", gTourismgl);
		}else {
			Tourismgl glid2 = tourismService.selGlByUidAndGlid(glid,uid);
			return Msg.success().add("info", glid2);
		}
	}
	/**
	 * 攻略详情评论
	 * @param glid
	 * @param page
	 * @param row
	 * @param uid
	 * @return
	 */
	@RequestMapping("/glcomments")
	public Msg findGlComments(String glid,int page,int row,String uid) {
		if (glid == null || glid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		if (uid == null || uid.isEmpty()) {
			PageInfo<GlComments> selByGlid = tourismService.selByGlid(glid,page,row);
			return Msg.success().add("comments", selByGlid);
		}else {
			PageInfo<GlComments> pageInfo = tourismService.selCommentsByGlid(glid,page,row,uid);
			return Msg.success().add("comments", pageInfo);
		}
		
	}
	/**
	 * 官方攻略列表
	 * @param glclass
	 * @return
	 */
	@RequestMapping("/gfgl")
	public Msg findByGlclass(int[] glclass,String city) {
		if (glclass == null || glclass.length<=0 || city == null || city.isEmpty()) {
			return Msg.fail("入参错误");
		}
		List<Map<Object, Object>> list = new ArrayList<>();
		Map<Object, Object> map = new HashMap<>();
		for (int i = 0; i < glclass.length; i++) {
			 List<Tourismgl> selByglass = tourismService.selByglass(glclass[i],city);
			 map.put(glclass[i], selByglass);
		}
		list.add(map);
		return Msg.success().add("list", list);
	}
	/**
	 * 攻略-更多-必玩
	 * @param glclass
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/more")
	public Msg findMore(String glclass,int page,int row) {
		if (glclass == null || glclass.isEmpty()) {
			return Msg.fail("入参失败");
		}
		PageInfo<Tourismgl> selByGlClass = tourismService.selByGlClass(glclass,page,row);
		return Msg.success().add("selByGlClass", selByGlClass);
		
	}
	

}
